package xunlei;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.internal.mozilla.IIDStore;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import com.sun.net.ssl.HttpsURLConnection;
import com.sun.org.apache.xerces.internal.util.URI.MalformedURIException;


/*
 *@author 邓强
 *@软件2班
 */
public class Task implements Runnable {
	private String urlPath;
	private String savePath;
	private Table table;
	private String fileName;
	private TableItem ti;
	private long size = 0;
	private long totalSize = 0;
	private ProgressBar bar;
	boolean pause = false;
	public Task(String urlPath, String savePath, Table table) {
		this.urlPath = urlPath;
		this.fileName = urlPath.substring(urlPath.lastIndexOf("/") + 1);
		this.savePath = savePath + "\\" + fileName;
		this.table = table;
	}

	@Override
	public void run() {
		Display.getDefault().asyncExec(new Runnable() {

			@Override
			public void run() {
				ti = new TableItem(table, SWT.NONE);
				ti.setText(0, fileName);
				ti.setText(1, urlPath);
				ti.setText(2, savePath);
				ti.setText(4,"正在下载");

				TableEditor editor = new TableEditor(table);
				bar = new ProgressBar(table, SWT.NONE);
				bar.setMinimum(0);
				bar.setMaximum(100);

				editor.grabHorizontal = true;
				editor.grabVertical = true;

				editor.setEditor(bar, ti, 3);

			}
		});

		FileOutputStream fos = null;
		InputStream is = null;
		HttpURLConnection con = null;

		try {
			URL url = new URL(urlPath);

			con = (HttpURLConnection) url.openConnection();
			con.setRequestMethod("GET");
			con.setConnectTimeout(5*1000);

			con.connect();

			totalSize = con.getContentLengthLong();

			is = con.getInputStream();
			fos = new FileOutputStream(savePath);

			byte[] bt = new byte[1024];
			int len = -1;
			while( !pause && (len = is.read(bt)) != -1) {
				fos.write(bt, 0, len);
				fos.flush();

				size += len;

				Display.getDefault().syncExec(new Runnable() {

					@Override
					public void run() {
						bar.setSelection((int)((double)size / totalSize * 100));					
					}
				});
			}
			fos.flush();
			if(!pause) {
				Display.getDefault().syncExec(new Runnable() {

					@Override
					public void run() {
						ti.setText(4,"下载完成");

					}
				});
			}
		}catch (MalformedURIException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			if(fos != null) {
				try {
					fos.flush();
				}catch (IOException e) {
					e.printStackTrace();
				}
			}

			if(is != null) {
				try {
					is.close();
				}catch (IOException e) {
					e.printStackTrace();
				}
			}

			if(con != null) {
				con.disconnect();
			}
		}
	}

	public void start() {
		notify();
	}
}
